package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.Employee;
import model.Envelope;
import view.HRUpEmpDetGui;
import view.HRcreateNewEmployeeGui;
import view.HumanResourcesGui;

/**
 * this class is the controller of human resources employee
 * @author gili
 *
 */
public class HRcontroller extends AbstractTransfer {

	Employee HRmodel;	
	HumanResourcesGui HRview;
	LoginController prevController;
	/**
	 * 
	 * @param hrm is the model of employee
	 * @param hrv is the view, i.e. the human resources gui
	 * @param lastCon is the previous controller
	 */
	public HRcontroller(Employee hrm, HumanResourcesGui hrv,LoginController lastCon){
		  super(hrv,hrm);
		  HRview =  hrv;
		  HRmodel = hrm;
		  prevController = lastCon;
		  //connect buttons to their action listeners 
		  HRview.addLogoutActionListener(new LogOutListener());
		  HRview.addcreateNewEmployeeActionListener(new CreateNewEmployeeListener());
		  HRview.addUpdateDetalesActionListener(new UpdateDetalesListener());
		}
	
	public HRcontroller getHRcontroller(){
		return this;
	}
	
	public Employee getHRmodel() {
		return HRmodel;
	}

	public HumanResourcesGui getHRview() {
		return HRview;
	}

	public LoginController getPrevController() {
		return prevController;
	}

	/**
	 * this is a listener to the log out button
	 * it sends to the server the details of the human resources employee connected to the system
	 * @author gili
	 *
	 */
	class LogOutListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

			Envelope en1 = new Envelope(HRmodel,"updateStatus0");
			sendToServer(en1);

			HRview.dispose();
			prevController.getLoginG().ClearText();
			prevController.getLoginG().setVisible(true);
			
		}

	}
	/**
	 * this is a listener for create new employee button
	 * it closes current gui and opens the relevant window
	 * @author gili
	 *
	 */
	class CreateNewEmployeeListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub	
				HRview.dispose();
				HRcreateNewEmployeeGui newEmpGui = new HRcreateNewEmployeeGui();
				HRcreateNewEmployeeController con = new HRcreateNewEmployeeController(newEmpGui,getHRcontroller());
			}	  
		 }
	/**
	 * this is a listener for update employee details button
	 * it closes current gui and opens the relevant window
	 * @author gili
	 *
	 */
	class UpdateDetalesListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HRview.dispose();
				HRUpEmpDetGui updateGui = new HRUpEmpDetGui();
				HRupdateEmployeeDetalesController con = new HRupdateEmployeeDetalesController(updateGui,getHRcontroller());
			}	  
		 }

}


